package com.mzxy.mztopline;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.mzxy.mztopline.adapter.MyFragmentPagerAdapter;
import com.mzxy.mztopline.fragment.CountFragment;
import com.mzxy.mztopline.fragment.HomeFragment;
import com.mzxy.mztopline.fragment.MeFragment;
import com.mzxy.mztopline.fragment.VideoFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "yidong1731";
    private TextView tvMainTitle;
    private RadioGroup radioGroup;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvMainTitle = findViewById(R.id.tv_main_title);
        tvMainTitle.setText("首页");
        radioGroup = findViewById(R.id.radioGroup);

        viewPager = findViewById(R.id.viewPager);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new CountFragment());
        fragments.add(new VideoFragment());
        fragments.add(new MeFragment());

        FragmentPagerAdapter adapter =
                new MyFragmentPagerAdapter(getSupportFragmentManager(),fragments);

        viewPager.setAdapter(adapter);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_home:   //点击首页
                        //TODO
                        Toast.makeText(MainActivity.this,"首页",Toast.LENGTH_SHORT).show();
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.rb_count:  //点击统计
                        //TODO
                        Log.d(TAG,"点击了统计");
                        Toast.makeText(MainActivity.this,"统计",Toast.LENGTH_SHORT).show();
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.rb_video:  //视频
                        //TODO
                        Toast.makeText(MainActivity.this,"视频",Toast.LENGTH_SHORT).show();
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.rb_me:  //个人中心
                        Toast.makeText(MainActivity.this,"个人中心",Toast.LENGTH_SHORT).show();
                        viewPager.setCurrentItem(3);
                        //TODO
                        break;
                }
            }
        });

        viewPager.setOffscreenPageLimit(3); //三个界面之间来回切换都不会重新加载数据。
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.d(TAG,"onPageSelected  ####### position:"+position);
                switch (position){
                    case 0:
                        radioGroup.check(R.id.rb_home);
                        tvMainTitle.setText("首页");
                        break;
                    case 1:
                        radioGroup.check(R.id.rb_count);
                        tvMainTitle.setText("统计");

                        break;
                    case 2:
                        radioGroup.check(R.id.rb_video);
                        tvMainTitle.setText("视频");

                        break;
                    case 3:
                        radioGroup.check(R.id.rb_me);
                        tvMainTitle.setText("我");

                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });



    }



    //1. 监听返回按钮

    long lastTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            //判断上次点击时间 和本次的时间差是否小于3秒
            long time = (System.currentTimeMillis()-lastTime);
            Log.d("mzxy","onKeyDown time:"+time);
            if(time < 2*1000){
                Log.d("mzxy","onKeyDown exit");
                MainActivity.this.finish();
                System.exit(0);
            }else{
                Log.d("mzxy","onKeyDown save time");
                Toast.makeText(MainActivity.this,"再按一次退出！",Toast.LENGTH_SHORT).show();
                lastTime = System.currentTimeMillis();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
